//
//  MeatMapShopAddressViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/2.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatMapShopAddressViewController.h"
#import "MeatSearchHeader.h"
#import "MeatTipLabel.h"
#import "MeatMapLocationView.h"
#import "MeatMapFooter.h"
#import "MeatMapTableViewCell.h"
#import "XXTGSearchAreaView.h"


@interface MeatMapShopAddressViewController ()

@property (nonatomic, strong) UIButton *rightNav;

@property (nonatomic, strong) MeatSearchHeader *searchHeader;
@property (nonatomic, strong) MeatTipLabel *tipLabel;
@property (nonatomic, strong) MeatMapLocationView *locationView;
@property (nonatomic, strong) XXTGSearchAreaView *searchAreaView;

@end

@implementation MeatMapShopAddressViewController

#pragma mark - lifecycle methods

- (void)setupUI
{
    
    {
        self.baseNavView.navTitleLabel.text = @"选择配送范围";
        self.view.backgroundColor = KWhiteColor;
        
        [self.baseNavView.rightNavBtn setTitle:@"新增地址" forState:UIControlStateNormal];
        self.baseNavView.rightNavBtn.hidden = NO;
        self.baseNavView.rightNavBtn.width = 65;
        self.baseNavView.rightNavBtn.mj_x = KScreenWidth - self.baseNavView.rightNavBtn.width -5;

    }
    
    {
        [self.view addSubview:self.searchHeader];
        [self.view addSubview:self.tipLabel];
        //[self.view addSubview:self.locationView];

        self.searchAreaView.hidden = YES;
        [self.view addSubview:self.searchAreaView];
    }

}


- (void)setupLayout
{
    {
        _searchHeader.mj_y = TJ_NavBarHeight;
        _searchHeader.navView.mj_x = 15;
        _searchHeader.navView.mj_y = 5;
        _searchHeader.navView.width = KScreenWidth - 30;
        _searchHeader.height = 45;
    }
    
    {
        _tipLabel.mj_y = _searchHeader.bottom + 3;
       // _locationView.mj_y = _tipLabel.bottom;
    }
    
    {
        self.tableView.mj_y = _tipLabel.bottom;
        self.tableView.height = KScreenHeight - (_tipLabel.bottom);
    }
    
    {
        self.searchAreaView.frame = CGRectMake(0, TJ_StatusBarHeight_Define, KScreenWidth, KScreenHeight - TJ_StatusBarHeight_Define);

    }
}


#pragma mark - networking methods
#pragma mark - response methods
#pragma mark - public methods
#pragma mark - tableview.delegate methods


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return section == 0 ?3 :5;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 34;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, KScreenWidth, 34);
    view.backgroundColor = mBackgroudColor;
    
    UILabel *label = [UILabel makeLabelWithText:@"附近地址" textColor:UIColorFromRGB(0x969696) font:mFont(14) textAliment:0];
    label.size = CGSizeMake(100, 34);
    label.mj_y = 0;
    label.mj_x = 15;
    [view addSubview:label];
    
    return view;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 68;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeatMapTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatMapTableViewCell"];
    cell.isHiddenTip = indexPath.section == 0 ?NO :YES;
    cell.isHiddenRemind = (indexPath.section == 1 &&indexPath.row >3) ?NO :YES;

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - other methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
    CGFloat sectionHeaderHeight = 34;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}



#pragma mark - setter methods
#pragma mark - getter methods

- (void)registerTableViewCell{
    
    [self.tableView registerClass:[MeatMapTableViewCell class] forCellReuseIdentifier:@"MeatMapTableViewCell"];
    self.tableView.backgroundColor = KWhiteColor;
    self.tableView.separatorColor = mIconLineColor;
    self.tableView.rowHeight = 68;
    self.tableView.tableHeaderView = self.locationView;
    MeatMapFooter *footer = [[MeatMapFooter alloc] init];
    self.tableView.tableFooterView = footer;
}

- (UIButton *)rightNav{
    if (!_rightNav) {
        _rightNav = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [_rightNav setTitle:@"新增地址" forState:UIControlStateNormal];
        [_rightNav setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
        _rightNav.titleLabel.font = mFont(13);
        [_rightNav setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        //[_rightNav addTarget:self action:@selector(meatCartEdit:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightNav;
}


- (MeatSearchHeader *)searchHeader{
    
    if (!_searchHeader) {
        
        _searchHeader = [[MeatSearchHeader alloc] init];
        _searchHeader.leftNavBtn.hidden = YES;
        _searchHeader.searchTextField.placeholder = @"输入您的收货地址";
        
        @weakify(self);
        _searchHeader.tapBlock = ^{
            @strongify(self);
            
        };
        
    }
    return _searchHeader;
}


- (MeatTipLabel *)tipLabel{
    if (!_tipLabel) {
        _tipLabel = [[MeatTipLabel alloc] init];
        _tipLabel.height = 34;
        _tipLabel.text = @"当前不在配送范围内，请选择您的准确地址";
    }
    return _tipLabel;
}


- (MeatMapLocationView *)locationView{
    
    if (!_locationView) {
        _locationView = [[MeatMapLocationView alloc] init];
    }
    return _locationView;
}


- (XXTGSearchAreaView *)searchAreaView{
    if (!_searchAreaView) {
        _searchAreaView = [[XXTGSearchAreaView alloc]initWithFrame:CGRectZero];
    }
    return _searchAreaView;
}


@end
